<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller{
	public function _initialize(){
		//验证登陆,没有登陆则跳转到登陆页面
		if(empty($_SESSION['username'])) $this->redirect('Admin/Login/index');
		$this->db_config();
		// dump(session());
		// dump(C('DB_DEPOT'));
		
		//加载自定义配置信息
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
		$list=$m->where(array('status'=>1))->getField('en_name,value');
		C('base_config',$list);
		//加载列表菜单
		$m=M('AuthRule',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('AuthGroupAccess',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('AuthGroup',C('DB_PRS'),C('DB_DEPOT'));
		$uid=session('uid');
		$group_access=$n->where(array('uid'=>$uid))->find();
		//列表菜单筛选条件
		if($group_access['group_id']!=1){//非超级管理员，加载权限菜单节点
			$rules=$g->where(array('id'=>$group_access['group_id']))->find();
			$map['id']=array('in',$rules['rules']);
		}
		$map['pid']=0;
		$map['status']=1;
		$map['is_menu']=1;
		// dump($m);
		$Menu = $m->where($map)->order("o")->select();
        foreach($Menu as $v=>$value){
			$map['pid']=$value['id'];
            $Menu[$v]['voo'] = $m->where($map)->order("o")->select();
        }
		$c_name=CONTROLLER_NAME;
		$a_name=ACTION_NAME;
		$p_id=$m->where(array('status'=>1,'is_menu'=>1,'contr'=>$c_name,'func'=>$a_name))->order("id,o")->find();
		//加载页面菜单
		$menu_page=$m->where(array('status'=>1,'is_menu'=>1,'pid'=>$p_id['id']))->order("id,o")->select();

		$this->assign("Menu",$Menu);
		$this->assign("menu_page",$menu_page);
		
		//常用列表
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		$storage_lists=storage_list();//库位列表
		$coin_lists=coinlist();//币制列表
		$country_lists=countrylist();//国家列表
		$unit_lists=unitlist();//单位列表
		$contactscompany_lists=contactsCompanylist();//往来单位列表
		
		// dump($section_lists);
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));
		$this->assign('storage_lists',json_encode($storage_lists));
		$this->assign('coin_lists',json_encode($coin_lists));
		$this->assign('country_lists',json_encode($country_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('contactscompany_lists',json_encode($contactscompany_lists));
		
		// $this->checkAuth();
		///////权限验证
		// if(!authCheck(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME,session('uid'))){
			// header('HTTP/1.1 404 Not Found');
			// $return['message']='你没有权限';
			// $return['status']=false;
			// $this->ajaxReturn($return);			
		// }
	}
	public function checkAuth(){
		$uid=session('uid');
		$ac=M('AuthGroupAccess',C('DB_PRS'),C('DB_DEPOT'));
		$ag=M('AuthGroup',C('DB_PRS'),C('DB_DEPOT'));
		$ar=M('AuthRule',C('DB_PRS'),C('DB_DEPOT'));
		
		$group_id=$ac->getField('group_id');
		$rules=$ag->where(array('id'=>$group_id))->getField('rules');
		$rules_arr=explode(',',$rules);
		$ar_id=$ar->where(array('contr'=>CONTROLLER_NAME,'func'=>ACTION_NAME))->getField('id');
	
	
		if(!in_array($ar_id,$rules_arr) and !empty($ar_id)){
			header('HTTP/1.1 404 Not Found');
			$return['message']='你没有权限';
			$return['status']=0;
			$this->ajaxReturn($return);
		}
		
	}
    protected function _empty(){
        //$this->error('你请求的页面不存在!!');
        echo "<script>$.messager.alert('错误提示','你请求的页面不存在!!','error');</script>";
    }
	
	/*
	 *获取数据库配置
     */
    public function db_config(){
		
		$info=M('Warehouse')->where(array('warehouse_code'=>session('warehouse_code')))->find();//获取配置表中对应域名的id
		// echo M()->getlastsql();
		// dump($info);
		if($info){  //天众数据库库配置
			!empty($info['db_ip'])?$db_ip=$info['db_ip']:$db_ip=C('DEPOT_DB_HOST');
			!empty($info['db_name'])?$db_name=$info['db_name']:$db_name=C('DEPOT_DB_NAME');
			!empty($info['db_username'])?$db_username=$info['db_username']:$db_username=C('DEPOT_DB_USER');
			!empty($info['db_password'])?$db_password=$info['db_password']:$db_password=C('DEPOT_DB_PWD');
			!empty($info['db_port'])?$db_port=$info['db_port']:$db_port=C('DEPOT_DB_PORT');
			!empty($info['db_pre'])?$db_pre=$info['db_pre']:$db_pre=C('DEPOT_DB_PREFIX');
			
			//当前仓库数据库配置
			C('DB_DEPOT.DB_TYPE','mysql');
			C('DB_DEPOT.DB_HOST',$db_ip);
			C('DB_DEPOT.DB_NAME',$db_name);
			C('DB_DEPOT.DB_USER',$db_username);
			C('DB_DEPOT.DB_PWD',$db_password);
			C('DB_DEPOT.DB_PORT',$db_port);
			C('DB_PRS',$db_pre);
			C('DB_DEPOT.DB_CHARSET','utf8');
			
			
			//权限验证设置
			C('AUTH_CONFIG.AUTH_ON',true);
			C('AUTH_CONFIG.AUTH_TYPE',1);
			C('AUTH_CONFIG.AUTH_GROUP',C('DB_PRS').'auth_group');
			C('AUTH_CONFIG.AUTH_GROUP_ACCESS',C('DB_PRS').'auth_group_access');
			C('AUTH_CONFIG.AUTH_RULE',C('DB_PRS').'auth_rule');
			C('AUTH_CONFIG.AUTH_USER',C('DB_PRS').'auth_user');
		}
    }
}
?>